package com.aofeng.hybridwork.android.task;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.android.NativeBaseMethod;
import com.aofeng.hybrid.android.task.SimpleAsyncTask;
import com.aofeng.hybrid.service.EntityService;
import com.aofeng.hybrid.service.SqlService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LGY on 2016-09-17.
 */
public class FetchPaperTask extends SimpleAsyncTask{
    public FetchPaperTask(HybridActivity hybridActivity, JSONObject json) {
        super(hybridActivity, json);
    }

    @Override
    protected JSONObject action() throws Exception {
        JSONObject data  = json.getJSONObject("data");
        String f_check_item_id = data.getString("f_check_item_id");
        String f_checker_id = data.getString("f_checker_id");

        JSONObject paper = getPaper(f_check_item_id, f_checker_id, hybridActivity);
        if(paper == null) {
            paper = new JSONObject();
        }
        return paper;
    }

    public static JSONObject getPaper(String f_check_item_id, String f_checker_id, HybridActivity hybridActivity) throws JSONException {
        EntityService es = new EntityService(hybridActivity);
        SqlService ss = new SqlService(hybridActivity);
        JSONObject sql = new JSONObject();
        sql.put("sql","select * from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "'");
        JSONArray array = ss.action(sql).getJSONArray("data");
        if(array.length() == 0) {
            return null;
        }
        JSONObject paper = array.getJSONObject(0);
        sql.put("sql", "select * from t_check_paper_furnace where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_furnaces", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_heater where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_heaters", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_alarm where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_alarms", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_cooker where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_cookers", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_innerplumb where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_inner_plumbs", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_valve where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_valves", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_plumb where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_plumbs", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_meter where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_meters", ss.action(sql).getJSONArray("data"));
        sql.put("sql", "select * from t_check_paper_defect where f_paper_id in (select id from t_check_paper where f_check_item_id = '" + f_check_item_id + "' and f_checker_id= '" + f_checker_id + "')");
        paper.put("f_defects", ss.action(sql).getJSONArray("data"));

        paper.put("code", 200);
        return paper;
    }
}
